#!/bin/sh

#Usage: ./rpm_verification srcrpm_path

#src.rpm的build的操作
rpm_build()
{
	#获取当前路径
	now_path=$pwd
	#获取输入的src rpm路径
	srcrpm_path=$1 
	#echo "${srcrpm_path}"
	
	#搜索此目录下所以的src.rpm文件
	srcrpm_files=$(find "${srcrpm_path}" -type f -name "*.src.rpm")
	#echo "${srcrpm_files}"
	
	#SPECS目录,固定为用户目录下的rpmbuild
	spec_path=$HOME/rpmbuild/SPECS/
	
	#RPMS目录,固定为用户目录下的rpmbuild
	rpm_path=$HOME/rpmbuild/RPMS/x86_64/
	
	#记录src.rpm的数量
	local num=0 
	

	#判断rpmbuild工具是否安装,未安装则安装
	if command -v rpmbuild >/dev/null 2>&1;then
		true
	else
		echo 'rpmbuild no install'
		yum install -y rpm-build rpmdev* 
		if [ $? -ne 0 ];then
			echo "install rpm-build rpmdev* failed !!!"
			exit 0
		fi
	fi
	
	
	#循环处理每个src.rpm 包括
	#rpm -i src.rpm
	#rpmbuild -ba spec
	#测试install和remove
	for file in ${srcrpm_files}
	do
		num=$(($num+1))
		cd ${now_path}
		#获取src.rpm文件名
		echo -e "========= ${num} src rpm start build ========="
		file_name=${file##*/}
		echo -e "src rpm: ${file_name}\n"
			
		rpm -i ${file}
		#安装src.rpm失败,清空rpmbuild,继续处理
		if [ $? -ne 0 ];then
			echo -e "rpm -i ${file} failed !!!\n"
			rm -rf $HOME/rpmbuild/
			continue
		fi

		#搜索spec文件
		cd ${spec_path}
		spec_file=$(find ./ -type f -name "*.spec")
		echo -e "SPEC: ${spec_file##*/}\n"
		
		echo y | rpmbuild -ba ${spec_file} --nodebuginfo 
		#编译spec失败,清空rpmbuild,继续处理
		if [ $? -ne 0 ];then
			echo -e "rpmbuild -ba ${spec_file} --nodebuginfo failed !!!\n"
			rm -rf $HOME/rpmbuild/
			continue
		fi
		
		install_remove_test	
		
		rpm_service_test
		
		rpm_cmd_help_test
		
		echo -e "\n\n"
		rm -rf $HOME/rpmbuild/


		
	done
}

#rpm包的安装卸载测试
install_remove_test()
{
	echo -e "========= Start ${rpm_name} Install and Remove Test ========="	
	
	#搜索rpm包,确定包名
	cd ${rpm_path}
	rpm_file=$(find "./" -type f -name "*.rpm")
	rpm_name_version=${rpm_file##*/}
	echo -e "RPM: ${rpm_name_version}\n"
	rpm_name=$(echo "${rpm_name_version}" | sed -e s/-[[:digit:]]./@/|  awk -F '@' '{print $1}')
	
	
	installed_rpm_name=${rpm_name_version%.*}
	#echo "${installed_rpm_name}"
	
	
	#安装rpm包,已安装需卸载
	if [[ $(rpm -qa ${rpm_name}) = "${installed_rpm_name}" ]];then
		echo -e "${installed_rpm_name} has beend installed,now remove it\n"
		rpm -e ${installed_rpm_name}
	else
		echo -e "${installed_rpm_name} no installed,now install it\n"
	fi
	
	echo -e "start install test.........\n"
	#执行安装
	rpm -ivh ${rpm_name_version}
	
	#判断是否安装成功,成功后则测试卸载,失败则退出测试
	if [ $? -eq 0 -a $(rpm -qa ${rpm_name}) = "${installed_rpm_name}" ];then
		echo -e 'Install Status: Success\n'
		
		echo -e "start remove test.........\n"
		#执行卸载
		yum remove -y ${rpm_name}
		if [ $? -eq 0 ];then
			echo -e 'Remove Status: Success\n'
		else
			rm -rf $HOME/rpmbuild/
			echo -e 'Remove Status: Fail\n'
			exit 0
		fi
	else
		rm -rf $HOME/rpmbuild/
		echo -e 'Install Status: Fail\n'
		exit 0
	fi	
}

#rpm包中service测试,即service启停与状态
rpm_service_test()
{
	#安装rpm包
	cd ${rpm_path}
	rpm -ivh ${rpm_name_version} >/dev/null 2>&1
	
	echo -e "========= Start ${rpm_name} Service Start and Stop Test ========="	
	
	#搜索rpm包中包含的service
	if [ $(rpm -ql ${rpm_name} | grep -E "\.service$" | awk -F "/" '{print $NF}' | wc -l) -ne 0 ];then
		rpm_services_list=$(rpm -ql ${rpm_name} | grep -E "\.service$" | awk -F "/" '{print $NF}')
		echo -e "${rpm_name} Service: ${rpm_services_list}\n"
	else
		echo -e "${rpm_name} Has no Service, So do not Service Start and Stop Test !!!\n"
		break
	fi
	
	#记录service的数量
	local service_num=0 
	
	#循环测试每个service的启停
	for service in ${rpm_services_list}
	do
		service_num=$(($service_num+1))
		echo -e "${num} ${service} start test.........\n"
		#启动service
		systemctl start ${service}
		if [ $? -eq 0 ];then
			echo -e 'Start ${service} Status: Success\n'
			
			echo -e "{num} ${service} status test.........\n"
			#查看service状态
			systemctl status ${service}
			if [ $? -eq 0 ];then
				echo -e 'Test ${service} Status: Success\n'
			else
				echo -e 'Test ${service} Status: Fail\n'
				continue
			fi
			
			echo -e "{num} ${service} stop test.........\n"
			#停止service
			systemctl stop ${service}
			if [ $? -eq 0 ];then
				echo -e 'Stop ${service} Status: Success\n'
			else
				echo -e 'Stop ${service} Status: Fail\n'
				continue
			fi
		else
			echo -e 'Start ${service} Status: Fail\n'
			continue
		fi	
	done
}

#rpm包中指令的--help测试,即兼容性测试
rpm_cmd_help_test()
{	
	echo -e "========= Start ${rpm_name} CMD --help Test ========="	
	#搜索rpm包中包含的指令
	if [ $(rpm -ql ${rpm_name} | grep -E '/s?bin/' | awk -F "/" '{print $NF}' | wc -l) -ne 0 ];then
		rpm_cmd_list=$(rpm -ql ${rpm_name} | grep -E '/s?bin/' | awk -F "/" '{print $NF}')
		echo -e "${rpm_name} CMD: ${rpm_cmd_list}\n"
	else
		echo -e "${rpm_name} Has no CMD, So do not CMD --help Test !!!\n"
		break
	fi
	
	#记录cmd的数量
	local cmd_num=0 
	
	#循环测试每个cmd --help
	for cmd in ${rpm_cmd_list}
	do
		cmd_num=$(($cmd_num+1))
		echo -e "${num} ${cmd} start --help test.........\n"
		#启动cmd -h
		${cmd} --help
		if [ $? -eq 0 ];then
			echo -e 'Start ${cmd} --help Status: Success\n'
		else
			echo -e 'Start ${cmd} --help Status: Fail\n'
			continue
		fi	
	done
}




#清空rpmbuild及log日志
rm -rf $HOME/rpmbuild/ 
rm -rf ./log
	
rpm_build "$1" | tee ./log
